package com.tang.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tang.common.entity.community.Article;

import com.tang.community.mapper.ArticleMapper;
import com.tang.community.mapper.CommentMapper;
import com.tang.community.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Article> listArticle(String keywords, Integer currentPage, Integer pageSize, Integer hasAuth) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Article> qw = new QueryWrapper<>();
            qw.eq("delete_flag", 1);
            qw.eq("state", hasAuth);
            qw.orderByDesc("create_time");
            List<Article> articles = articleMapper.selectList(qw);
           setCommentNumberFromTable(articles);
            return articles;
        }
        QueryWrapper<Article> queryWrapper = new QueryWrapper<Article>();
        queryWrapper.eq("delete_flag", 1).orderByDesc("create_time").
                eq("state", hasAuth).like("article_title", keywords).or().like("user_name", keywords);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        setCommentNumberFromTable(articles);
        return articles;
    }

    @Override
    public List<Article> listMyArticle(Integer currentPage, Integer pageSize, String userName) {
        PageHelper.startPage(currentPage, pageSize);
        QueryWrapper<Article> qw = new QueryWrapper<>();
        qw.eq("delete_flag", 1);
        qw.eq("user_name", userName);
        qw.orderByDesc("create_time");
        List<Article> articles = articleMapper.selectList(qw);
        setCommentNumberFromTable(articles);
        return articles;
    }

    public void setCommentNumberFromTable(List<Article> articles){
        if(!articles.isEmpty()){
            for (Article art : articles) {
                QueryWrapper q = new QueryWrapper();
                q.eq("article_id", art.getArticleId());
                Integer integer = commentMapper.selectCount(q);
                art.setCommentNumber(integer);
            }
        }
    }

    @Override
    public String auditArticle(Integer articleId, String res) {
        Article article = articleMapper.selectById(articleId);
        if ("pass".equals(res) && article != null) {
            article.setState(1);
            int i = articleMapper.updateById(article);
            if (i != 0) {
                return "pass";
            }
        }
        if ("noPass".equals(res) && article != null) {
            article.setState(2);
            int i = articleMapper.updateById(article);
            if (i != 0) {
                return "noPass";
            }
        }
        return null;
    }
}
